package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.education.common.Result;
import com.yqg.education.entity.TeachEvaluate;
import com.yqg.education.mapper.TeachEvaluateMapper;
import com.yqg.education.service.ITeachEvaluateService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TeachEvaluateController 前端控制器
 * @author zxf
 * @since 2025-04-15
 */
@RestController
@RequestMapping("/teach-evaluate")
public class TeachEvaluateController {

    @Resource
    private ITeachEvaluateService teachEvaluateService;

    @Resource
    private TeachEvaluateMapper teachEvaluateMapper;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody TeachEvaluate teachEvaluate) {
        teachEvaluate.setCreateTime(LocalDateTime.now());
        return Result.success(teachEvaluateService.saveOrUpdate(teachEvaluate));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        TeachEvaluate teachEvaluate =teachEvaluateService.getById(id);
        teachEvaluate.setIsDelete(1);
        return Result.success(teachEvaluateService.saveOrUpdate(teachEvaluate));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<TeachEvaluate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<TeachEvaluate> list=  teachEvaluateService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(teachEvaluateService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                           Integer teachId,
                           Integer stuId,
                           String evaluate) {
        pageNum = pageNum * pageSize - pageSize;
        List<TeachEvaluate> teachEvaluateList = teachEvaluateMapper.findPage(pageNum, pageSize, teachId, stuId, evaluate);
        Long count = teachEvaluateMapper.findCount(teachId, stuId, evaluate);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", teachEvaluateList);
        map.put("total", count);
        return Result.success(map);
    }

    /**
     * 学员端获取教师评价
     */
    @GetMapping("/stuFindTeacherEvaluation")
    public Result stuFindTeacherEvaluation(@RequestParam Integer pageNum,
                                          @RequestParam Integer pageSize){
        Integer pageStartNumber = pageNum * pageSize - pageSize;
        Map<String,Object> map = new HashMap<>();
        List<Map<String, Object>> teacherEvaluationList = teachEvaluateMapper.findTeacherEvaluation(pageStartNumber, pageSize);
        Integer count = teachEvaluateMapper.findTeacherEvaluationCount();
        map.put("records",teacherEvaluationList);
        map.put("total", count);
        return Result.success(map);
    }

}

